package com.data.dbdao;

import com.data.util.FinalStringUtils;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;

@Repository
@Transactional
public class FillMassageDao extends BaseDao {

    /**
     * 保存账号申请 的详细信息
     *
     * @param ms
     * @param rm
     * @return
     */
    public boolean saveMassageDao(Object ms, Object rm) {
        return super.saveMassageDao(ms, rm);
    }

    /**
     * @param type
     * @param id
     * @param email
     * @param code
     * @return
     */
    public boolean changeAccountState(int type, String id, String email, String code) {
        String hql = reHql(type);
        hql += " set p.estate=?,p.code=? where p.id=? and p.email=? and p.code=?";
        return super.updateOneMssageDao(hql, true, null, id, email, code);

    }


    /**
     * 设置账号类型(个人，或者公司用户)
     *
     * @param id    id
     * @param types 身份类型
     * @return
     */
    public boolean updateAccountTypeDao(String id, int types) {
        String sql = "update Account set types=? where id=?";
        return super.updateOneMssageDao(sql, types, id);
    }

    /**
     * 重发邮件的实现
     *
     * @param id
     * @param email
     * @param type
     * @param code
     * @return
     */
    public boolean updateSendMailDao(String id, String email, int type, String code) {
        String hql = reHql(type);
        if (hql != null) {
            hql += " set p.code=?,p.ctime=? where p.id=? and p.email=?";
            return super.updateOneMssageDao(hql, code, new Date(), id, email);
        }
        return false;
    }

    /**
     * @param type 账号身份类型
     * @return
     */
    private String reHql(int type) {
        String hql;
        switch (type) {
            case FinalStringUtils.PERSON:
                hql = "update PersonMassage as p ";
                break;
            case FinalStringUtils.COMPANY:
                hql = "update CompanyMassage as p ";
                break;
            default:
                return null;
        }
        return hql;
    }

    /**
     * 更换邮箱
     *
     * @param id
     * @param email
     * @param type
     * @param code
     * @return
     */
    public boolean changeMailDao(String id, String email, int type, String code) {
        String hql = reHql(type);
        if (hql != null) {
            hql += " set p.code=?,p.ctime=?,p.email=? where p.id=?";
            return super.updateOneMssageDao(hql, code, new Date(), email, id);
        }
        return false;
    }
}
